package Matter;

import Form.INode;

public class LinkQueue implements INode {
    private Node front;
    private Node rear;
    public  LinkQueue() {
        front = rear = null;
    }

    @Override
    public void clear() {
        front=rear=null;
    }

    public boolean isEmpty() {
        return front == null;
    }
    public  int length() {
        Node p = front;              //队首指针
        int length = 0;              //队尾指针
        //链队列类的构造函数
        while (p != null) {
            p = p.next;              //指针下移
            ++length;                // 计数器加一
        } return length;
    } //取队首元素
    public Object peek() {
        if (front != null)            //队列为空
            return  front.data;       //返回队首结点的数据域值
        else
            return null;
    } //入队
    public void offer(Object x) {
        Node p = new Node(x);          //初始化新结点
        if (front !=null) {            //队列非空
            rear.next = p;
            rear = p;                  //改变队尾的位置
        }
        else
             front = rear = p;
    } //出队
    public  Object poll() {
        if (front != null){           //队列非空
        Node p = front;               //p指向队首结点
        front = front.next;           //队首结点出列
        if (p == rear)                //被删除的结点是队尾结点时
             rear = null;
        return p.data;                //返回队首结点的数据域值
         }else
            return null;

    }

}


